package com.guohao.http;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.util.Map;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ServerMapping implements HttpHandler {

  private final Map<String, MethodInfo> requestInfoMap;
  private final ThreadPoolExecutor poolExecutor;

  public ServerMapping(Map<String, MethodInfo> requestInfoMap) {
    this.requestInfoMap = requestInfoMap;
    poolExecutor = new ThreadPoolExecutor(500, 1000,
        60L, TimeUnit.SECONDS,
        new SynchronousQueue<>());
  }

  @Override
  public void handle(HttpExchange exchange) {
    poolExecutor.execute(new ServerHandler(exchange, requestInfoMap));
  }
}
